package com.btl.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.btl.utils.PraseResponse;

public class HttpServies {
	/**
	 * post方式提交到服务器
	 * 
	 * @param path
	 *            提交路径
	 * @param params
	 *            待提交参数
	 * @param encode
	 *            参数值编码
	 * @return 返回值
	 * @throws Exception
	 *             可以出现的异常
	 */
	public static Object SendPostRequest(String path,
			Map<String, String> params, String encode, int i) throws Exception {

		// 根据params集合构建要传输到服务器端的data
		StringBuilder data = new StringBuilder();
		// 迭代参数集合
		for (Map.Entry<String, String> entry : params.entrySet()) {
			data.append(entry.getKey()).append('=')
					.append(URLEncoder.encode(entry.getValue(), encode))
					.append('&');
		}
		
		if( data != null && data.length() >0){
			data.deleteCharAt(data.length() - 1);
		}
		//data.deleteCharAt(data.length() - 1);
		// title=aaa&content=bbb
		byte[] entity = data.toString().getBytes(); // 封装了我们要传到服务器端的数据
		
		HttpURLConnection conn = (HttpURLConnection) new URL(path)
				.openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("POST"); //大写
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		conn.setRequestProperty("Content-Length", entity.length + "");
		conn.setDoOutput(true); //允许对提交数据
		//通过conn得到一个输出流，这个输出流就是用于提交数据
		OutputStream outputStream=conn.getOutputStream();
		outputStream.write(entity);
		if (conn.getResponseCode() == 200) {
			InputStream inStream=conn.getInputStream();
			switch(i){
			case 1:
				return true;
			case 2:
				
				return PraseResponse.parseVerification(inStream);
			case 3:
				return PraseResponse.parsePerson(inStream);
			
			}
			
		}
		return null;
	}

	/**
	 * get方式提交
	 * 
	 * @param path
	 * @param params
	 * @param encode
	 * @return
	 * @throws Exception
	 */
	public static boolean SendGetRequest(String path,
			Map<String, String> params, String encode) throws Exception {
		
		StringBuilder url = new StringBuilder(path);
		url.append('?');
		// 迭代参数集合
		for (Map.Entry<String, String> entry : params.entrySet()) {
			url.append(entry.getKey()).append('=')
					.append(URLEncoder.encode(entry.getValue(), encode))
					.append('&');
		}
		url.deleteCharAt(url.length() - 1);

		HttpURLConnection conn = (HttpURLConnection) new URL(url.toString())
				.openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");

		if (conn.getResponseCode() == 200) {
			return true;
		}
		return false;
	}
	
	
	public static Bitmap getBitmap(String imagePath) { 

	    HttpGet get = new HttpGet(imagePath); 
	    HttpClient client = new DefaultHttpClient(); 
	    Bitmap pic = null; 
	    try { 
	        HttpResponse response = client.execute(get); 
	        HttpEntity entity = response.getEntity(); 
	        InputStream is = entity.getContent(); 
	       
	        pic = BitmapFactory.decodeStream(is);   // 关键是这句代码  

	    } catch (ClientProtocolException e) { 
	        e.printStackTrace(); 
	    } catch (IOException e) { 
	        e.printStackTrace(); 
	    } 
	    return pic; 
	}
	
	public static Bitmap getBitmap2(String imagePath) { 

	    HttpGet get = new HttpGet(imagePath); 
	    HttpClient client = new DefaultHttpClient(); 
	    Bitmap pic = null; 
	    try { 
	        HttpResponse response = client.execute(get); 
	        HttpEntity entity = response.getEntity(); 
	        InputStream is = entity.getContent(); 
	        BitmapFactory.Options options = new BitmapFactory.Options();
			options.inSampleSize = 4;
	        pic = BitmapFactory.decodeStream(is,null, options);   // 关键是这句代码  

	    } catch (ClientProtocolException e) { 
	        e.printStackTrace(); 
	    } catch (IOException e) { 
	        e.printStackTrace(); 
	    } 
	    return pic; 
	}
}
